package com.xbongbong.saas.model.impl;/*/*/

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.WorkOrderFlowUserDao;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.domain.entity.WorkOrderFlowUserEntity;
import com.xbongbong.saas.enums.subform.WorkOrderTeamEnum;
import com.xbongbong.saas.model.WorkOrderFlowUserModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/11 16:42
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderFlowUserModel")
public class WorkOrderFlowUserModelImpl implements WorkOrderFlowUserModel {
    @Resource
    private WorkOrderFlowUserDao workOrderFlowUserDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public Integer insert(WorkOrderFlowUserEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer result = workOrderFlowUserDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
        paasEsModel.insert(saveDTO);
//        paasEsModel.insert(entity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity.getCorpid(), entity.getId(), entity.getDataId());
        return result;
    }

    @Override
    public Integer update(WorkOrderFlowUserEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        Integer update = workOrderFlowUserDao.update(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
        paasEsModel.update(saveDTO);
//        paasEsModel.update(entity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity.getCorpid(), entity.getId(), entity.getDataId());
        return update;
    }

    @Override
    public List<WorkOrderFlowUserEntity> findEntitys(Map<String, Object> param) {
        return workOrderFlowUserDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workOrderFlowUserDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<String> getTeamUserIdList(Long workOrderId, String corpid, Integer isMain) {
        return workOrderFlowUserDao.getTeamUserIdList(workOrderId, corpid, isMain);
    }

    @Override
    public void delete(Map<String, Object> param) throws XbbException {
        workOrderFlowUserDao.delete(param);
        List<WorkOrderFlowUserEntity> workOrderFlowUserEntities = findEntitys(param);
        for (WorkOrderFlowUserEntity entity : workOrderFlowUserEntities) {
            paasEsModel.delete(entity.getId(), entity.getCorpid(), IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity.getDataId());
        }
    }

    @Override
    public void delete(WorkOrderFlowUserEntity entity) throws XbbException {
        entity.setDel(1);
        entity.setUpdateTime(DateTimeUtil.getInt());
        workOrderFlowUserDao.update(entity);
        paasEsModel.delete(entity.getId(), entity.getCorpid(),IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity.getDataId());
    }

    @Override
    public Integer batchDeleteByWorkOrderId(String corpid, List<Long> workOrderIdIn, List<String> userIdList) throws XbbException {
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("del", 0);
        map.put("dataIdIn", workOrderIdIn);
        if (userIdList != null) {
            map.put("userIdIn", userIdList);
        }
        List<WorkOrderFlowUserEntity> workOrderFlowUserEntities = findEntitys(map);
        Integer result = workOrderFlowUserDao.batchDeleteByWorkOrderId(corpid, workOrderIdIn, userIdList);
        for (WorkOrderFlowUserEntity entity : workOrderFlowUserEntities) {
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, entity.getDataId());
        }
        return result;
    }

    @Override
    public void insertBatch(List<WorkOrderFlowUserEntity> workOrderFlowUserEntityList) throws XbbException {
        insertBatch(workOrderFlowUserEntityList, WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }

    @Override
    public void insertBatch(List<WorkOrderFlowUserEntity> workOrderFlowUserEntityList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        workOrderFlowUserDao.insertBatch(workOrderFlowUserEntityList);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, workOrderFlowUserEntityList);

        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM));
        }

        paasEsModel.insertBatch(insertDTOList, refreshPolicy);
    }

    @Override
    public List<UserTeamEntity> findBaseEntitys(Map<String, Object> map) {
        return workOrderFlowUserDao.findBaseEntitys(map);
    }

    @Override
    public Integer save(WorkOrderFlowUserEntity entity) throws XbbException {
        if (entity.getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public WorkOrderFlowUserEntity getByKey(Long key, String corpid) {
        return workOrderFlowUserDao.getByKey(key, corpid);
    }

    @Override
    public List<Long> getTeamIds(Long workOrderId, String corpid, Integer isMain) {
        return workOrderFlowUserDao.getTeamIds(workOrderId, corpid, isMain);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del) throws XbbException {
        workOrderFlowUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(WorkOrderTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        workOrderFlowUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(WorkOrderTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy);
    }

    @Override
    public List<WorkOrderFlowUserEntity> getUserListByWorkorderId(String corpid, Long id) {
        return workOrderFlowUserDao.getUserListByWorkorderId(corpid, id);
    }

    @Override
    public void deleteBatchById(List<Long> ids, String corpid) {
        workOrderFlowUserDao.deleteBatchById(ids, corpid, DelEnum.DELETE.getDel());
    }

    @Override
    public void updateBatch(List<WorkOrderFlowUserEntity> list, String corpid) throws XbbException {
        Integer success = workOrderFlowUserDao.updateBatch(list, corpid);

        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM, list);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM));
        }
        paasEsModel.updateBatch(insertDTOList);
    }

    @Override
    public void deleteBatch(List<WorkOrderFlowUserEntity> userEntities, String corpid, Integer del) throws XbbException {

        List<Long> ids = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
        map.put(StringConstant.DEL, del);
        for (WorkOrderFlowUserEntity entity : userEntities){
            ids.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM.getType());
            insertDTO.setSource(map);
            insertDTOList.add(insertDTO);
        }
        workOrderFlowUserDao.deleteBatchById(ids, corpid, del);

        paasEsModel.updateBatch(insertDTOList);
    }

    @Override
    public List<Long> getWorkOrderIdByUserIdIn(String corpid, List<String> userIds, Integer isMain) {
        return workOrderFlowUserDao.getWorkOrderIdByUserIdIn(corpid, userIds, isMain);
    }
}
